// index.js
// 获取应用实例
const app = getApp()
import {
  getIndexGoods,
  getUserLogin
} from '../../apis/api'
Page({
  onShareAppMessage: function() {
    return {
      title: '崇明味道',
      path: '/pages/index/index',
    }
  },
  onShareTimeline: function() {
    return {
      title: '崇明味道'
    }
  },
  data: {
    // 轮播图url
    imgUrls: [
      'http://xcx.jinchenghealth.com:8003/bgp/rr1.jpg',
      'http://xcx.jinchenghealth.com:8003/bgp/rr2.jpg',
      'http://xcx.jinchenghealth.com:8003/bgp/rr3.jpg'
    ],
    // 商品分类图
    goodsCatImgs: [
      'http://xcx.jinchenghealth.com:8003/bgp/veg.png',
      'http://xcx.jinchenghealth.com:8003/bgp/meet.png',
      'http://xcx.jinchenghealth.com:8003/bgp/fish.png',
      'http://xcx.jinchenghealth.com:8003/bgp/rice.png',
      'http://xcx.jinchenghealth.com:8003/bgp/egg.png',
      'http://xcx.jinchenghealth.com:8003/bgp/best2.png',
      'http://xcx.jinchenghealth.com:8003/bgp/icon.png',
      'http://xcx.jinchenghealth.com:8003/bgp/fruit.png',
      'http://xcx.jinchenghealth.com:8003/bgp/bird.png',
      'http://xcx.jinchenghealth.com:8003/bgp/mmeat.png',
      '../../images/free.png'
    ],
    // 商品
    vegetableList: [],
    meetList: [],
    eggList: [],
    riceList: [],
    fishList: [],
    bestList: [],
    cornList: [],
    fruitList: [],
    birdList: [],
    meetliveList: [],
    freeList: [],

    // 是否显示面板指示点
    indicatorDots: true,
    // 是否自动切换
    autoplay: true,
    // 自动切换时间间隔    
    interval: 3000,
    // 滑动动画时长    
    duration: 1000,
    // 轮播图高度
    Height: "",
    token: '',
    isLog: 0,
    text: "10斤大米礼盒装兑换卡、20斤大米提货卡兑换时间晚于15:00,会在隔日安排配送,请谅解",
  },
  // onLoad: async function() {
  //   this.setData({
  //     token: wx.getStorageSync('token'),
  //   })
  //   await this.getLog()
  //   await this.getGoods()
  // },
  onShow: async function () {

    wx.setStorageSync('vegetableList', [])
    wx.setStorageSync('meetList', [])
    wx.setStorageSync('eggList', [])
    wx.setStorageSync('riceList', [])
    wx.setStorageSync('fishList', [])
    wx.setStorageSync('bestList', [])
    wx.setStorageSync('cornList', [])
    wx.setStorageSync('fruitList', [])
    wx.setStorageSync('birdList', [])
    wx.setStorageSync('meetliveList', [])
    wx.setStorageSync('freeList', [])
    wx.showShareMenu({
      withShareTicket:true,
      menu:['shareAppMessage','shareTimeline']
    })
    this.setData({
      token: wx.getStorageSync('token'),
    })
    this.getLog()
    await this.getGoods()
  },
 
  getLog: function () {
    if (!!!wx.getStorageSync('token')) wx.setStorageSync('token', 'token null')
    getUserLogin().then(res => {
      if (res.code === 200) {
        this.setData({
          isLog: res.data
        })
      }
    })
  },
  // 设置轮播图自适应高度
  imgHeight: function (e) {
    // 获取当前屏幕的宽度
    var winWid = wx.getSystemInfoSync().windowWidth;
    // 图片高度
    var imgh = e.detail.height;
    // 图片宽度
    var imgw = e.detail.width;
    // 等比设置swiper的高度
    var swiperH = winWid * imgh / imgw + "px"
    this.setData({
      Height: swiperH
    })
  },
  // 获取商品
  async getGoods() {
    const res = await getIndexGoods({
      dishName: '',
      currentPage: 1,
      pageSize: 999
    })
    console.log(res);
    const mList = []
    const vList = []
    const eList = []
    const fList = []
    const bList = []
    const rList = []
    const cList = []
    const ffList = []
    const bbList = []
    const mmList = []
    const fffList = []
    if (!!res.data) {
      for (let i = 0; i < res.data.length; i++) {
        if (res.data[i].type === "肉" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&mList.length < 4) mList.push(res.data[i])
        }
        if (res.data[i].type === "蛋" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&eList.length < 4) eList.push(res.data[i])
        }
        if (res.data[i].type === "蔬菜" && res.data[i].commodityQuantity != 0) {
          if(res.data[i].homepage && vList.length < 4){
            vList.push(res.data[i])
          }
        }
        if (res.data[i].type === "大米" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&rList.length < 4) rList.push(res.data[i])
        }
        if (res.data[i].type === "水产" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&fList.length < 4) fList.push(res.data[i])
        }
        if (res.data[i].type === "特产" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&bList.length < 4) bList.push(res.data[i])
        }
        if (res.data[i].type === "玉米" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&cList.length < 4) cList.push(res.data[i])
        }
        if (res.data[i].type === "水果" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&ffList.length < 4) ffList.push(res.data[i])
        }
        if (res.data[i].type === "禽类" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&bbList.length < 4) bbList.push(res.data[i])
        }
        if (res.data[i].type === "畜肉" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&mmList.length < 4) mmList.push(res.data[i])
        }
        if (res.data[i].type === "赠品" && res.data[i].commodityQuantity !== 0) {
          if (res.data[i].homepage &&fffList.length < 4) fffList.push(res.data[i])
        }
      }
    }
    this.setData({
      vegetableList: vList,
      meetList: mList,
      eggList: eList,
      fishList: fList,
      bestList: bList,
      riceList: rList,
      cornList: cList,
      fruitList: ffList,
      birdList: bbList,
      meetliveList: mmList,
      freeList: fffList,
    })
  },
  // 搜索商品
  searchGoods() {

  },
  // 跳转激活界面
  goActive: async function () {
    if (!!this.data.token && this.data.isLog) {
      wx.navigateTo({
        url: '/pages/active/active',
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '激活功能需要登录，是否前往登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/tomeslog/tomeslog',
            })
          }
        }
      })
    }
  },
  // 跳转购卡界面
  goBuyCard: async function () {
    // if(!!this.data.token && this.data.isLog) {
    //   wx.navigateTo({
    //     url: '/pages/buycard/buycard',
    //   })
    // }else {
    //   wx.showModal({
    //     title: '提示',
    //     content: '购卡功能需要登录，是否前往登录',
    //     success(res) {
    //       if(res.confirm) {
    //         wx.navigateTo({
    //           url: '/pages/tolog/tolog',
    //         })
    //       }
    //     }
    //   })
    // }
    // able
    // wx.showModal({
    //   title: '提示',
    //   content: '购卡功能暂未开放',
    //   showCancel: false
    // })
    wx.switchTab({
      url: '/pages/myvip/myvip',
    })
  },
  // 跳转下单界面
  goBuy() {
    if (this.data.token && this.data.isLog) {
      wx.navigateTo({
        url: '/pages/buy/buy',
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '下单功能需要登录，是否前往登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/tolog/tolog',
            })
          }
        }
      })
    }
  },
  swPage() {
    wx.switchTab({
      url: '/pages/Tabone/Tabone',
      success: res => {
        console.log(res);
      }
    })
  },
  swNormalPage() {
    wx.navigateTo({
      url: '/page/login/login',
    })
  },
  backPage() {
    wx.navigateBack({
      delta: 1
    })
  },
  onReachBottom: function () {}
})